(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			getAllRoll();
		});
	});
	function pagebtns(){
		check("user-cellphone",checkMobilePhone,"checkAddNull");
		check("user-email",checkEmail,"check");
		checkPwd("user-password","user-confirm-password");
		check("user-alias",isNull,"null");
		check("user-student",isNull,"null");
		check("user-usercode",isNull,"null");
		$("#user-add").click(function(){
			if(checkUser() && check2("check-fail")){
				addUser();
			}
		});
		$("#user-cellphone").bind({
			blur:function(){
				if($("#user-cellphone").val().length != 0 ){
					var usercode = $("#user-cellphone").val();
					if(checkMobilePhone(usercode)){
						$("#user-usercode").val(usercode);
						$("#user-usercode").focus();
					}else{
						$("#user-usercode-ex").html("不能为空");
					}
				}
			},
			focus:function(){
				if($("#user-cellphone").val().length != 0 ){
					$("#user-usercode").val("");
					$("#user-usercode-ex").html("");
				}
			}
		});
		
		$("#choice-student").unbind("click");
		$("#choice-student").click(function(){
			var studentcodes = $("#user-student").attr("studentcodes").split(",");
			studentChoice(function(){
				$("#student-choice-name",parent.parent.document).show();
				$("#student-choice-submit",parent.parent.document).show();
				$(".selectL-title",parent.parent.document).text("待选学生");
				$(".selectR-title ",parent.parent.document).text("已选学生");
				$(".student-choice-title-name",parent.parent.document).text("选择学生");
				studentSeachMenu(studentcodes);
				selectLR();
				$("#sub",parent.parent.document).unbind("click");
				$("#sub",parent.parent.document).click(function(){
					var selVal = [];
					var studentCode = [];
					var imei = [];
					var rightSel = $("#selectR",parent.parent.document);
					rightSel.find("option",parent.parent.document).each(function(){
						selVal.push(this.value);
						studentCode.push($(this).attr("studentcode"));
						imei.push($(this).attr("imei"));
					});
					selVals = selVal.join(",");
					studentCodes = studentCode.join(",");
					for(var key in imei){
						if(imei[key] == undefined || imei[key] == "undefined" || imei[key] == null || imei[key] == "null"){
							imei[key] = "null";
						}
					}
					imeis = imei.join(",");
					$(".index-dialog-cover",parent.parent.document).hide();
					$("#student-choice-dialog",parent.parent.document).hide();
					$("#user-student").empty();
					$("#user-student").val(selVals);
					$("#user-student").attr("studentcodes",studentCodes);
					$("#user-student").attr("imeis",imeis);
					$("#user-student").focus();
					$("#selectR",parent.parent.document).empty();
				});
				$("#student-choice-submit",parent.parent.document).unbind("click");
				$("#student-choice-submit",parent.parent.document).click(function(){
					initStudents(1,100000,studentcodes);
				});
				$("#student-choice-name",parent.parent.document).unbind("keyup");
				$("#student-choice-name",parent.parent.document).keyup(function(){
					this.value=this.value.replace(/^ +| +$/g,'');
				});
				document.onkeydown = function(e){ 
				    var ev = document.all ? window.event : e;
				    if(ev.keyCode==13) {
				    	initStudents(1,100000,studentcodes);
				    }
				};
				
			});
		});
	}
	function addUser(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var params = {};
		var param = {};
		params["alias"] = encodeURI($("#user-alias").val());	
		params["region"] = encodeURI($("#user-region").val());
		params["address"] = encodeURI($("#user-address").val());
		params["cellphone"] = $("#user-cellphone").val();
		params["email"] = encodeURI($("#user-email").val());		
		params["remark"] = encodeURI($("#user-remark").val());			
		params["usercode"] = encodeURI($("#user-usercode").val());
		params["password"] = encodeURI($("#user-password").val());
		params["usertype"] = encodeURI($("#user-usertype option:selected").val());
		params["schoolcode"] = schoolcode;
		if($("#user-usertype option:selected").val() == "0"){
			params["code"] = encodeURI($("#user-code option:selected").val());
		}else if($("#user-usertype option:selected").val() == "1"){
			params["studentcode"] = encodeURI($("#user-student").attr("studentcodes"));
			params["stuimeis"] = encodeURI($("#user-student").attr("imeis"));
		}
		params["islock"] = true;
		for(var key in params){
			if(params[key] != ""){
				param[key] = params[key];
			}
		}
		var data = {
				cmd:"webRegister",
				model:"user",
				userKey:userKey,
				data:param,
				remark:"0",						
		};
		processBegin("正在添加中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("添加用户成功");
			}else{
				processError(res.error);
			}
		},null,null);	
	}
	function getAllRoll(){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getRole",
				model:"license",
				userKey:userKey,
				data:{
					schoolcode:schoolcode,
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data.data;
				if(data.length > 0){
					for(var i = 0; i < data.length; i++){
						content += "<option value = '"+data[i].code+"'>"+data[i].name+"</option>";
					}
				}else{
					content += "<option>- - -</option>";
				}
			}else{
				content += "<option>- - -</option>";
			}
			$("#user-code").html(content);
		});
	}
	function checkUser(){
		if($("#user-student-td").css("display") != "none"){
			if($("#user-student").val() == ""){
				$("#user-student-ex").html("不能为空");
				return false; 
			}else if($("#user-cellphone").val() == ""){
				$("#user-cellphone-ex").html("不能为空");
				return false; 
			}else if($("#user-usercode").val() == ""){
				$("#user-usercode-ex").html("不能为空");
				return false; 
			}else if($("#user-password").val() == ""){
				$("#user-password-ex").html("不能为空");
				return false; 
			}else if($("#user-confirm-password").val() == ""){
				$("#user-confirm-password-ex").html("不能为空");
				return false; 
			}else if($("#user-alias").val() == ""){
				$("#user-alias-ex").html("不能为空");
				return false;			
			}else{
				return true;
			}
		}else{
			if($("#user-cellphone").val() == ""){
				$("#user-cellphone-ex").html("不能为空");
				return false; 			
			}else if($("#user-usercode").val() == ""){
				$("#user-usercode-ex").html("不能为空");
				return false; 
			}else if($("#user-password").val() == ""){
				$("#user-password-ex").html("不能为空");
				return false; 
			}else if($("#user-confirm-password").val() == ""){
				$("#user-confirm-password-ex").html("不能为空");
				return false; 
			}else if($("#user-alias").val() == ""){
				$("#user-alias-ex").html("不能为空");
				return false; 
			}else{
				return true;
			}
		}
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
	
})();
